package com.ruoyi.mentor.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mentor.bo.SubjectAddBo;
import com.ruoyi.mentor.bo.SubjectEditBo;
import com.ruoyi.mentor.bo.SubjectQueryBo;
import com.ruoyi.mentor.domain.Subject;
import com.ruoyi.mentor.mapper.SubjectMapper;
import com.ruoyi.mentor.service.ISubjectService;
import com.ruoyi.mentor.vo.SubjectVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 学科，记录学科信息Service业务层处理
 *
 * @author LH
 * @date 2021-05-21
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements ISubjectService {

    @Override
    public SubjectVo queryById(Long id){
        return getVoById(id, SubjectVo.class);
    }

    @Override
    public TableDataInfo<SubjectVo> queryPageList(SubjectQueryBo bo) {
        PagePlus<Subject, SubjectVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SubjectVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SubjectVo> queryList(SubjectQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SubjectVo.class);
    }

    private LambdaQueryWrapper<Subject> buildQueryWrapper(SubjectQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Subject> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getMajorName()), Subject::getMajorName, bo.getMajorName());
        lqw.like(StrUtil.isNotBlank(bo.getSubName()), Subject::getSubName, bo.getSubName());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SubjectAddBo bo) {
        Subject add = BeanUtil.toBean(bo, Subject.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SubjectEditBo bo) {
        Subject update = BeanUtil.toBean(bo, Subject.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(Subject entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    /**
     * 批量导入学科信息
     *
     * @param file
     */
    @Override
    @Transactional
    public void importData(MultipartFile file) throws Exception {
        ExcelUtil<SubjectVo> util = new ExcelUtil<SubjectVo>(SubjectVo.class);
        List<SubjectVo> subjectList = util.importExcel(file.getInputStream());
        List<Subject> collect = subjectList.stream().map(item -> {
            return BeanUtil.toBean(item, Subject.class);
        }).collect(Collectors.toList());
        saveBatch(collect);
    }
}
